<?php
/**
 * Dispute abstraction.
 *
 * @category Zend
 * @package Dispute
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 * @license GPL v3
 */

class Application_Model_Dispute extends Disputeo_Model_Table_Row
{
    /**
     * @var string
     */
    protected static $_dbTable = 'dispute';
    
    /**
     * @var string
     */
    protected $_creator;
    
    /**
     * @var string
     */
    protected $_debator;
    
    /**
     * @var string
     */
    protected $_category;
    
    /**
     * @var int
     */
    protected $_posts;
    
    /**
     * @var int
     */
    protected $_views;
    
    /**
     * @var int
     */
    protected $_score;
    
    /**
     * Get text for the speech bubble
     * 
     * @return string
     */
    public function getBubbleText()
    {
        if (strlen($this->argument) <= 360) {
            return $this->argument;
        }
        
        $pos = strpos($this->argument, ' ', 360);
        return substr($this->argument, 0, $pos) . '...';
    }
    
    /**
     * Get HTML version of the argument text
     * 
     * @return string
     */
    public function getArgumentHtml()
    {
        return Disputeo_Markdown::convert($this->argument);
    }
    
    /**
     * Get bit of text indicating how much time has passed since post
     * 
     * @return string
     */
    public function getTimePassed()
    {   
        $measure = $this->_getTimeMeasure();
        
        $parts = array(
            Zend_Measure_Time::SECOND => 60,
            Zend_Measure_Time::MINUTE => 60,
            Zend_Measure_Time::HOUR => 24,
            Zend_Measure_Time::DAY => 365,
            Zend_Measure_Time::MONTH => null
        );
        
        foreach ($parts as $part => $unit) {
            $measure->convertTo($part);
            $diff = $measure->getValue();
            if ($diff < $unit || is_null($unit)) {
                $num = floor($diff);
                $text = $num > 1 ? strtolower($part) . 's' : strtolower($part);
                return $num . ' ' . $text;
            }
        }
    }
    
    /**
     * Get the measurement of time in seconds between created date and now
     * 
     * @return \Zend_Measure_Time
     */
    protected function _getTimeMeasure()
    {
        $date = new Zend_Date();
        $created = new Zend_Date($this->created);
        $diff = $date->sub($created)->toValue();
        $measure = new Zend_Measure_Time($diff, Zend_Measure_Time::SECOND);
        
        return $measure;
    }
    
    /**
     * Get a dispute object by primary key
     * 
     * @param int $id
     * @return Application_Model_Dispute
     */
    public static function get($id)
    {
        $db = self::_model();
        $select = $db->select()
            ->from('dispute', 
                array('disputeId', 'userId', 'title', 'argument', 'created', 'score'))
            ->join('category', 'dispute.categoryId = category.categoryId',
                array('category'))
            ->join(array('u' => 'user'), 'dispute.userId = u.userId', 
                array('creator' => 'userName'))
            ->where('disputeId = ?', $id);
        $row = $db->getTable()->getAdapter()->fetchRow($select);
        return new self($row);
    }
}
